//
//  ViewController.m
//  APItest
//
//  Created by Rain on 16/6/22.
//  Copyright © 2016年 Rain. All rights reserved.
//

#import "ViewController.h"
#import <AFHTTPSessionManager.h>

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 请求的参数
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"1025", @"oid", @"300", @"chargeMon",nil];
    
    NSString *url=[NSString stringWithFormat:@"http://192.168.1.189:8888/property/app_and/appQueryActualChargeMon"];
    
    // 初始化Manager
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    // 不加上这句话，会报“Request failed: unacceptable content-type: text/plain”错误，因为我们要获取text/plain类型数据
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    // post请求
    [manager POST:url parameters:dic constructingBodyWithBlock:^(id _Nonnull formData) {
        // 拼接data到请求体，这个block的参数是遵守AFMultipartFormData协议的。
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        // 这里可以获取到目前的数据请求的进度
    } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
        // 请求成功，解析数据
        NSLog(@"%@", responseObject);
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
        NSLog(@"%@", dic);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        // 请求失败
        NSLog(@"%@", [error localizedDescription]);
    }];
    
    // Get请求
//    [manager GET:@"http://192.168.1.189:8888/property/app_and/appQueryActualChargeMon?oid=1025&chargeMon=300" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
//        // 这里可以获取到目前的数据请求的进度
//    } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
//        // 请求成功，解析数据
//        NSLog(@"%@", responseObject);
//        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
//        
//        NSLog(@"%@", dic);
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        // 请求失败
//        NSLog(@"%@", [error localizedDescription]);
//    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
